//
//  RW_LoginViewModel.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_LoginViewModel.h"
#import "RW_InputCollectionViewCell.h"
#import "RW_ButtonCollectionViewCell.h"
#import "RW_ItemButtonCollectionViewCell.h"
#import "RW_AgreementCollectionViewCell.h"

@implementation RW_LoginViewModel
#pragma mark -- 逻辑处理
/** 提交操作 */
-(void)netWorkLayerModel:(WLBaseCollectionViewLayerModel *)layerModel
                 success:(void(^)(id response))success
               fail:(void(^)(NSError *error))fail{
    NSMutableDictionary *paramsMap = [NSMutableDictionary dictionary];
    
    Cell_Button_Type type = Cell_Button_Type_Default;
    BOOL isBreak = NO;
    for (WLBaseCollectionViewLayerSection *section in layerModel.viewLayer) {
        for (WLBaseCollectionViewLayerRow *row in section.item) {
            if ([row.data isKindOfClass:RW_InputCollectionViewCellModel.class]){
                RW_InputCollectionViewCellModel *model = row.data;
                
                if (model.input_text_string.length && model.parametersKey.length){
                    [paramsMap setValue:model.input_text_string forKey:model.parametersKey];
                } else {
                    [SVProgressHUD showError_wl:[NSString stringWithFormat:@"%@未填写",model.title_string]];
                    isBreak = YES;
                    break;
                }
                
                if ([model.parametersKey isEqualToString:@"account"]){
                    if (model.input_text_string.length < 6 || model.input_text_string.length > 15){
                        [SVProgressHUD showError_wl:@"账号长度错误！"];
                        isBreak = YES;
                        break;
                    }
                }
                if ([model.parametersKey isEqualToString:@"password"]){
                    if (model.input_text_string.length < 6 || model.input_text_string.length > 15){
                        [SVProgressHUD showError_wl:@"密码长度错误！"];
                        isBreak = YES;
                        break;
                    }
                }
                if ([model.parametersKey isEqualToString:@"nickName"]){
                    if (model.input_text_string.length < 1 || model.input_text_string.length > 9){
                        [SVProgressHUD showError_wl:@"昵称长度错误！"];
                        isBreak = YES;
                        break;
                    }
                }
            }
            if ([row.data isKindOfClass:RW_ButtonCollectionViewCellModel.class]){
                RW_ButtonCollectionViewCellModel *model = row.data;
                type = model.button_type;
            }
            if ([row.data isKindOfClass:RW_AgreementCollectionViewCellModel.class]){
                RW_AgreementCollectionViewCellModel *model = row.data;
                if (model.item_type == Agreement_Item_Type_Register){
                    if (!model.isSelect){
                        [SVProgressHUD showError_wl:@"请同意用户协议"];
                        isBreak = YES;
                        break;
                    }
                }
            }
        }
        if (isBreak){
            break;
        }
    }
    if (isBreak){
        return;
    }
    NSLog(@"参数 ： %@",paramsMap);
    //登陆请求
    if (type == Cell_Button_Type_Login){
        [[[RW_UserModel loginParamsMap:paramsMap] then:^id _Nullable(RW_UserModel * _Nullable value) {
            if (success){
                success(value);
            }
            return nil;
        }] catch:fail];
    }
    //注册请求
    else if (type == Cell_Button_Type_Register){
        [[[RW_UserModel registrParamsMap:paramsMap] then:^id _Nullable(id  _Nullable value) {
            if (success){
                success(value);
            }
            return nil;
        }] catch:fail];
    }
}


#pragma mark -- 视图模型获取
/** 获取视图模型 */
-(void)getViewSuccess:(void(^)(NSArray<WLBaseCollectionViewLayerSection *> *response))success
                  fail:(void(^)(NSError *error))fail{
    if (self.view_type == Login_View_Type_Login){
        if (success){
            success([self getLoginSection]);
        }
    } else if (self.view_type == Login_View_Type_Register){
        if (success){
            success([self getRegisterSection]);
        }
    } else if (self.view_type == Login_View_Type_AppleLogin){
        if (success){
            success([self getRegisterSection]);
        }
    }
}


#pragma mark -- 视图模型生成
/** 生成登陆视图 */
-(NSArray<WLBaseCollectionViewLayerSection *> *)getLoginSection{
    NSMutableArray *array = [NSMutableArray array];
    
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(10, 5, 0, 5);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 1;
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_Account;
            model.parametersKey = @"account";
            model.placeholder_string = @"请输入账号";
            model.title_string = @"账号";
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:50]]];
        }
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_PassWord;
            model.parametersKey = @"password";
            model.placeholder_string = @"请输入密码";
            model.title_string = @"密码";
            model.secureTextEntry = true;
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:50]]];
        }
        [array addObject:section];
    }
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(10, 10, 0, 10);
        section.horizontalSection = 0;
        section.horizontalCount = 4;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 10;
        
        CGFloat itemH = 30;
//        {
//            RW_ItemButtonCollectionViewCellModel *model = [[RW_ItemButtonCollectionViewCellModel alloc]init];
//            model.item_button_type = Cell_Item_Button_Type_Message;
//            model.title_string = @"短信登陆";
//            [section.item addObject:[RW_ItemButtonCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:itemH]]];
//        }
        [section.item addObject:[WLBaseCollectionViewCell getRow:nil cellSize:[section getCellSizeHeight:itemH]]];
        [section.item addObject:[WLBaseCollectionViewCell getRow:nil cellSize:[section getCellSizeHeight:itemH]]];
        [section.item addObject:[WLBaseCollectionViewCell getRow:nil cellSize:[section getCellSizeHeight:itemH]]];
        {
            RW_ItemButtonCollectionViewCellModel *model = [[RW_ItemButtonCollectionViewCellModel alloc]init];
            model.item_button_type = Cell_Item_Button_Type_Register;
            model.title_string = @"注册账号";
            [section.item addObject:[RW_ItemButtonCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:itemH]]];
        }
        [array addObject:section];
    }
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(10, 10, 0, 10);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 10;
        
        {
            RW_ButtonCollectionViewCellModel *model = [[RW_ButtonCollectionViewCellModel alloc]init];
            model.button_type = Cell_Button_Type_Login;
            model.title_string = @"登陆";
            [section.item addObject:[RW_ButtonCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:50]]];
        }
        [array addObject:section];
    }
    
    return array;
}

/** 生成注册视图 */
-(NSArray<WLBaseCollectionViewLayerSection *> *)getRegisterSection{
    NSMutableArray *array = [NSMutableArray array];
    
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(10, 0, 0, 0);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 1;
        
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_NickName;
            model.parametersKey = @"nickname";
            model.placeholder_string = @"请输入昵称";
            model.title_string = @"昵称";
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:40]]];
        }
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_Account;
            model.parametersKey = @"account";
            model.placeholder_string = @"请输入账号";
            model.title_string = @"账号";
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:40]]];
        }
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_PassWord;
            model.parametersKey = @"password";
            model.placeholder_string = @"请输入密码";
            model.title_string = @"密码";
            model.secureTextEntry = true;
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:40]]];
        }
        {
            RW_InputCollectionViewCellModel *model = [[RW_InputCollectionViewCellModel alloc]init];
            model.input_type = Cell_Input_Type_Verify_PassWord;
            model.parametersKey = @"passwordConfirm";
            model.placeholder_string = @"请输入密码";
            model.title_string = @"确认密码";
            model.secureTextEntry = true;
            [section.item addObject:[RW_InputCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:40]]];
        }
        [array addObject:section];
    }
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(20, 40, 0, 40);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 10;
        
        {
            RW_ButtonCollectionViewCellModel *model = [[RW_ButtonCollectionViewCellModel alloc]init];
            model.button_type = Cell_Button_Type_Register;
            model.title_string = @"注册";
            [section.item addObject:[RW_ButtonCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:50]]];
        }
        [array addObject:section];
    }
    
    {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(5, 40, 0, 40);
        section.horizontalSection = 0;
        section.horizontalCount = 3;
        section.horizontalMaxWidth = Main_Screen_Width;
        section.verticalSection = 10;
        
        {
            RW_AgreementCollectionViewCellModel *model = [[RW_AgreementCollectionViewCellModel alloc]init];
            model.item_type = Agreement_Item_Type_Register;
            
            [section.item addObject:[RW_AgreementCollectionViewCell getRow:model cellSize:[section getCellSizeHeight:30]]];
            [section.item addObject:[WLBaseCollectionViewCell getRow:nil cellSize:[section getCellSizeHeight:30]]];
            [section.item addObject:[WLBaseCollectionViewCell getRow:nil cellSize:[section getCellSizeHeight:30]]];
        }
        [array addObject:section];
    }
    
    return array;
}
@end
